<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.serial_code"
        placeholder="请输入单号"
        clearable
      ></el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-select v-model="search.status" placeholder="请选择状态">
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.mode" placeholder="请选择收取方向">
        <el-option
          v-for="item in modeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >

      <el-button icon="el-icon-circle-plus" type="primary" @click="addAllot"
        >添加费用单</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="serial_code"
        label="单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_name"
        label="供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer_name"
        label="采购员"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="rdc_name"
        label="配送中心"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="modeMsg"
        label="收取反向"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="billTypeMsg"
        label="单据类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="notes"
        label="备注"
        align="center"
      ></el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            v-if="scope.row.status != 2"
            @click="handleEdit(scope)"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status != 2"
            size="mini"
            type="primary"
            icon="el-icon-edit-outline"
            @click="confirms(scope)"
            >审核</el-button
          >
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            v-if="scope.row.status != 2"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 详情Dailog -->
    <particularsDialog :dialog="particularsDialog" @upData="getData"></particularsDialog>
    <!-- 添加、修改Dailog -->
    <expenseDocDialog :dialog="dialog" @upData="getData"></expenseDocDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import ParticularsDialog from "@/components/expenseDoc/particularsDialog";
import ExpenseDocDialog from "@/components/expenseDoc/expenseDocDialog";
import {
  expenseList,
  delExpense,
  examine
} from "@/api/expenseDoc/expenseDocList";
export default {
  components: {
    pagination: Pagination,
    expenseDocDialog: ExpenseDocDialog,
    particularsDialog: ParticularsDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },

      search: {
        serial_code: "",
        status: "",
        mode: "",
        start_time: "",
        end_time: ""
      },

      statusList: [
        {
          value: 0,
          label: "全部"
        },
        {
          value: 1,
          label: "待审核"
        },
        {
          value: 2,
          label: "已审核"
        }
      ],
      modeList: [
        {
          value: 1,
          label: "应收"
        },
        {
          value: 2,
          label: "应付"
        }
      ],
      //dialog信息
      dialog: {
        dialogVisible: false,
        id: ""
      },
      particularsDialog: {
        dialogVisible: false,
        id: ""
      },
      date: []
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        status: this.search.status,
        start_time: this.search.start_time,
        end_time: this.search.end_time,
        mode: this.search.mode,
        serial_code: this.search.serial_code
      };
      expenseList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              switch (res.status) {
                case 1:
                  res.statusMsg = "待审核";
                  break;
                case 2:
                  res.statusMsg = "已审核";
                  break;
              }
              switch (res.mode) {
                case 1:
                  res.modeMsg = "应收";
                  break;
                case 2:
                  res.modeMsg = "应付";
                  break;
              }
              switch (res.bill_type) {
                case 1:
                  res.billTypeMsg = "生鲜";
                  break;
                case 2:
                  res.billTypeMsg = "商超";
                  break;
              }
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    //删除
    handleDelete(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delExpense(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg || res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //审核
    confirms(scope) {
      let data = {
        id: scope.row.id
      };
      this.$confirm("是否审核信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          examine(data).then(res => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //添加
    addAllot() {
      this.dialog.dialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.id = "";
    },
    //编辑
    handleEdit(scope) {
      this.dialog.dialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.id = scope.row.id;
    },
    // 详情
    particulars(scope) {
      this.particularsDialog.dialogVisible = true;
      this.particularsDialog.id = scope.row.id;
    },
    //清空
    empty() {
      this.date = [];
      this.search = {
        serial_code: "",
        status: "",
        mode: "",
        start_time: "",
        end_time: ""
      };
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
